package com.gzqylc.docker.extend.auth.service;
import com.gzqylc.docker.extend.auth.bo.UserChangeBO;
import com.gzqylc.framework.modules.system.DataAuthority;
import com.gzqylc.framework.security.Subject;
import java.util.Collection;

import com.gzqylc.docker.extend.auth.dao.BaseUserDao;
import com.gzqylc.docker.extend.auth.domain.BaseUser;
import com.gzqylc.docker.extend.exception.AssertBusiness;
import com.gzqylc.framework.AjaxResult;
import com.gzqylc.framework.security.LoginSession;
import com.gzqylc.lang.tool.MD5Tool;
import com.gzqylc.lang.web.RequestTool;
import com.gzqylc.lang.web.jwt.Jwt;
import com.gzqylc.lang.web.jwt.JwtPayload;
import com.sun.org.apache.bcel.internal.generic.IFEQ;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Desc： 用户service
 * @ClassName BaseUserService
 * @Author 孔晓文
 * @Date 2022/2/19 上午9:13
 */
@Service
public class BaseUserService {

    @Autowired
    BaseUserDao baseUserDao;

    /**
     * @Desc  登录获取token
     * @MethodName login
     * @param loginName 登录名
     * @param password 登录密码
     * @Author 孔晓文
     * @Date 2022/2/21 上午9:05
     **/
  public Map<String, Object> login(HttpServletRequest request, String loginName, String password){

      BaseUser dbUser = baseUserDao.findOneByField(BaseUser.Fields.loginName, loginName);
      AssertBusiness.isActualTrue(dbUser == null,"用户不存在");

      //校验密码是否正确
      AssertBusiness.isActualTrue(!StringUtils.equals(dbUser.getPassWord(), MD5Tool.md5(password)),"密码错误!");

      JwtPayload payload = new JwtPayload();
      payload.setSub(dbUser.getId());

      Map<String, Object> data = new HashMap<>();
      String jwt = Jwt.createToken(payload);
      data.put("jwt", jwt);

      //上下文存储
      LoginSession loginSession = new LoginSession();
      loginSession.setLoginName(dbUser.getLoginName());
      loginSession.setUserId(dbUser.getId());
      LoginSession.set(jwt,loginSession);
      //记录登录时间
      dbUser.setLastLoginDate(new Date());
      String ip = RequestTool.getIpAddr(request);
      dbUser.setLastLoginIp(ip);
      baseUserDao.save(dbUser);
      return data;
  }

  /**
   * @Desc  修改用户信息
   * @MethodName update
   * @param bo
   * @Author 孔晓文
   * @Date 2022/3/3 下午2:30
   **/
  public void update(UserChangeBO bo){
      if (StringUtils.isNotEmpty(bo.getUserId())){
          BaseUser dbUser = baseUserDao.findOne(bo.getUserId());
          AssertBusiness.isActualTrue(dbUser == null,"用户不存在");
          //用户名
          if (StringUtils.isNotEmpty(bo.getUserName())){
              dbUser.setUserName(bo.getUserName());
          }
          //密码
          if (StringUtils.isNotEmpty(bo.getPassword())){
              dbUser.setPassWord(MD5Tool.md5(bo.getPassword()));
          }
          //邮箱地址
          if (StringUtils.isNotEmpty(bo.getEmailAddress())){
              dbUser.setEmailAddress(bo.getEmailAddress());
          }
          //登录名
          if (StringUtils.isNotEmpty(bo.getLoginName())){
              dbUser.setLoginName(bo.getLoginName());
          }
          baseUserDao.save(dbUser);
      }else {
          BaseUser baseUser = new BaseUser();
          BeanUtils.copyProperties(bo,baseUser);
          baseUserDao.save(baseUser);
      }

  }
  public Page<BaseUser> page(BaseUser baseUser, Pageable pageable){
      return baseUserDao.findAll(baseUser,pageable);
  }

  /**
   * @Desc  删除用户 后续做判断删除用户需要删除的东西
   * @MethodName delete
   * @param
   * @Author 孔晓文
   * @Date 2022/3/3 下午3:19
   **/
  public void delete(String userId){
      BaseUser dbUser = baseUserDao.findOne(userId);
      AssertBusiness.isActualTrue(dbUser == null,"用户不存在");
      baseUserDao.delete(dbUser);
  }

  /**
   * @Desc 登出请求
   * @MethodName logout
   * @param jwt jwt
   * @Author 孔晓文
   * @Date 2022/2/21 上午9:26
   **/
  public void logout(String jwt) {
      LoginSession loginSession = LoginSession.get(jwt);
      if (loginSession != null){
          loginSession.setUserId(null);
          LoginSession.set(jwt,loginSession);
      }
  }

}
